home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: fwrite help?
- Date: 18 Feb 1996 11:45:37 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4g73h1$1en@sparcserver.lrz-muenchen.de>
- References: <4fmaig$7of@news.global1.net> <harmon.824165113@pegasus.montclair.edu>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- harmon@pegasus.montclair.edu (Derek Harmon) writes:
-
- >youngrc@global1.net writes:
- >>What would the syntax for fwrite be to output 8 characters of
- >>char line[10] to a file? K&R doesn't write much about it. Thanks
- >>for the help.
-
- >size_t fwrite(const void *ptr, size_t size, size_t n, FILE *stream); is the
- >prototype I have. You can use line to devolve into a pointer towards the
- >first element in line, sizeof(char) for size insures compatibility on machines
- >where a character is more than one byte, and since you want 8 characters, set
- >n to 8.
-
- If sizeof(char) != 1, it's not C, it's somethig different. sizeof(char)
- is 1 by definition. sizeof returns the size of it's operand in chars,
- so, as far as the C programming language is concerned, there is _no_
- chance for sizeof(char) to be anything but 1.
-
- >: fwrite( line, sizeof(char), 8, fp);
-
- > Of course, when they are contiguous in an array, and you know the sizeof a
- >char is 1 byte, it's much more efficient to say you want to write an 8-byte
- >block of memory once, instead of a 1-byte block of memory eight times.
-
- Nothing prevents the implementation of fwrite() to treat
-
- fwrite(line, 1, 8, fp)
-
- as efficient as
-
- fwrite(line, 8, 1, fp)
-
- The only neccessary difference is the value returned on successful
- completion of the operation.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-